package com.lmb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lmb.bean.Category;
import com.lmb.bean.Dish;
import com.lmb.bean.DishFlavor;
import com.lmb.bean.R;
import com.lmb.dto.DishDto;
import com.lmb.service.CategoryService;
import com.lmb.service.DishFlavorService;
import com.lmb.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("/dish")
@RestController
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 菜品新增
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        //1.调用service处理
        dishService.saveWithFlavors(dishDto);

        //2.响应处理结果
        return R.success("新增成功！");
    }

    /**
     * 菜品分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(@RequestParam(defaultValue="1") Integer page, @RequestParam(defaultValue="10") Integer pageSize, String name){
        //1.调用service处理
        Page<DishDto> dishPage = dishService.findPage(page,pageSize,name);

        //2.响应处理结果
        return R.success(dishPage);
    }

    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id){
        //1.调用service处理
        DishDto dishDto = dishService.getByIdWithFlavors(id);

        //2.响应处理结果
        return R.success(dishDto);
    }

    /**
     * 菜品修改
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        //1.调用service处理
        dishService.updateWithFlavors(dishDto);

        //2.响应处理结果
        return R.success("修改成功！");
    }

    //删除菜品
    @DeleteMapping("/{ids}")
    public R<String> deleteByIds(@PathVariable Long[] ids){
        log.info("DishController-deleteByIds：ids={}",ids);

        //1.调用service处理
        dishService.deleteByIds(ids);

        //2.响应处理结果
        return R.success("删除成功！");
    }

    //根据菜品分类id和菜品名称查询菜品列表数据
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        //1.调用service处理，得到菜品列表数据
        //1.1：创建wrapper对象
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();

        //1.2：设置查询条件    【菜品所属分类id、菜品名称、菜品状态status】
        wrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId()); //当分类id不为null 根据分类id进行查询
        wrapper.like(dish.getName()!=null,Dish::getName,dish.getName());    //当菜品名称name不为null，根据菜品名称进行模糊查询

        /*=======1.添加查询条件  当status不为null时，设置查询启售的菜品列表数据======**/
        wrapper.eq(dish.getStatus()!=null,Dish::getStatus,dish.getStatus());  //查询启售的菜品

        //1.3：调用list方法查询
        List<Dish> list = dishService.list(wrapper);

        /*=======3.循环遍历List<Dish>集合，将菜品的基本信息封装到DishDto对象中======**/
        List<DishDto> dishDtoList = list.stream().map((item)->{
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            /*=======4.还需要根据菜品的分类id和菜品id查询出所属分类名称和菜品的口味信息封装到DishDto对象中======**/
            Category category = categoryService.getById(item.getCategoryId());
            dishDto.setCategoryName(category==null?"未知":category.getName());

            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId,item.getId());
            List<DishFlavor> flavors = dishFlavorService.list(wrapper1);
            dishDto.setFlavors(flavors);
            return dishDto;
        }).collect(Collectors.toList());


        /*=======5.将DishDto存入到List集合中 返回======**/
        //2.响应数据
        return R.success(dishDtoList);
    }

    //启用禁用
    @PostMapping("/status/{status}")
    public R<String> updateEmp(@PathVariable Integer status,@RequestParam List<Long> ids){
        dishService.updateDishStatus(status, ids);
        return R.success("修改成功！");
    }
}
